package com.java.test.audit.push;

import com.java.test.audit.dto.DepositExamineInfo;
import com.java.test.audit.dto.DrawingExamineInfo;
import com.java.test.audit.service.ExamineManageService;
import com.java.test.audit.websocket.PushExamineDeposit;
import com.java.test.audit.websocket.PushExamineDrawing;
import com.java.test.system.feesettings.FeeSettingsDAO;
import com.java.test.user.dto.PlayAccountInfo;
import com.java.test.user.service.MemberService;
import com.java.test.util.JsonUtil;
import com.java.test.util.MemberUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * TradeExamineResolving
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/22 0022 17:21
 */
@Slf4j
public class TradeExamineResolving {
	private ExamineManageService examineManageService;

	private MemberService memberservice;

	@Autowired
	private FeeSettingsDAO feeSettingsDAO;

	TradeExamineResolving(ExamineManageService examineManageService1,MemberService memberservice1) {
		examineManageService = examineManageService1;
		memberservice = memberservice1;
	}

	public void resolving(MessageExt msg) {
		System.out.println(msg.getKeys() + "==主动拉取==>" + new String(msg.getBody()) + msg.getTags());
		JSONObject message = new JSONObject(new String(msg.getBody()));
		if (!message.has("code")) {
			int orderClass = message.getInt("orderClass");
			String userId = message.getString("userId");// 付款账号
			PlayAccountInfo obj = MemberUtil.memberInfoMap.get(userId);
			if(obj==null){
				obj =memberservice.getMemberListByAccountName(userId);
			}
			if(orderClass==1){
				String fromCardId = message.getString("fromCardId");// 付款账户
				String fromName = message.getString("fromName");// 付款账户名称
				double amount = message.getDouble("amount");// 金额
				String toCardId = message.getString("toCardId");// 收款账户
				String toName = message.getString("toName");// 收款账户名称
				String orderId = message.getString("orderId");// 订单号
				long createTime = message.getLong("createTime");// 创建时间
				String payClass = message.getString("payClass");// 类型 100银行转账
				String toBankClassName = message.getString("toBankClassName");// 渠道缩写
				String toBankClassId = message.getString("toBankClassId");// 渠道id
				String yhproportion = examineManageService.selectPayManageSKAccount(toBankClassId,payClass,toCardId);
				//存款
				DepositExamineInfo info = new DepositExamineInfo();
				info.setDepositAmount(amount);// 存款金额 * 优惠比例

				Date date = new Date(createTime*1000);
				Instant instant = date.toInstant();
				ZoneId zoneId = ZoneId.systemDefault();
				LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

				info.setOrderCreateTime(localDateTime);// 订单时间
				info.setDepositTime(localDateTime);
				info.setDepositChannelValue(Integer.parseInt(toBankClassId));
				info.setDepositChannelName(toBankClassName);
				info.setOutbankAccount(fromCardId);
				info.setOutbankPeopleName(fromName);
				info.setIncomeAccount(toCardId);
				info.setMakecollections(toCardId);
				info.setOrderNumber(orderId);
				info.setContent(msg.getTags());
				info.setDiscountAmount((amount * Double.parseDouble(yhproportion)));
				info.setGameUniqueId(userId);
				if (obj != null) {
					info.setSliceValue(obj.getSliceValue());
					info.setSliceName(obj.getSliceName());
					info.setGameAccount(obj.getAccount());// 游戏账号
				}
				int i = examineManageService.saveDeposit(info);
				if (i > 0) {
					JSONObject jo = new JSONObject();
					jo.put("message", "ok");
					jo.put("code", 20000);
					try {
						jo.put("data", JsonUtil.obj2json(info));
						PushExamineDeposit.sendInfo(jo.toString(), null);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			}else if (orderClass==2){
				//提款
				String orderId = message.getString("orderId");
				String toCardId = message.getString("toCardId");
				double amount = message.getDouble("amount");// 金额
				long createTime = message.getLong("createTime");// 创建时间
				String toBankName = message.getString("toBankName");//用户名称
				int outClass = message.getInt("outClass");//提现渠道 100银行转账
				double serviceCharge = message.getDouble("serviceCharge");//手续费
				/*String toBankClassName = message.getString("toBankClassName");// 渠道缩写
				String toBankClassId = message.getString("toBankClassId");// 渠道id*/
				DrawingExamineInfo info = new DrawingExamineInfo();
				info.setDrawbankAccount(toCardId);
				info.setDrawingAmount(amount);
				info.setSxfCharge(serviceCharge);
				info.setDzjeAmount(amount-serviceCharge);
				Date date = new Date(createTime*1000);
				Instant instant = date.toInstant();
				ZoneId zoneId = ZoneId.systemDefault();
				LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
				info.setDrawingTime(localDateTime);// 订单时间
				info.setOrderNumber(orderId);
				info.setContent(msg.getTags());
				info.setDrawingChannelName(toBankName);
				info.setDrawingChannelValue(outClass);
				info.setCodeStatus(0);
				info.setGameUniqueId(userId);
				if (obj != null) {
					info.setSliceValue(obj.getSliceValue());
					info.setSliceName(obj.getSliceName());
					info.setGameAccount(obj.getAccount());// 游戏账号
				} else {
					info.setSliceValue(1);
					info.setSliceName("LV1");
					info.setGameAccount(userId);// 游戏账号
				}
				int i = examineManageService.saveDrawing(info);
				if (i > 0) {
					Double[] amounts = new Double[]{amount,amount};
					String account = info.getGameAccount();

					memberservice.updateFrozenAmount(amounts,account);
					JSONObject jo = new JSONObject();
					jo.put("message", "ok");
					jo.put("code", 20000);
					try {
						jo.put("data", JsonUtil.obj2json(info));
						PushExamineDrawing.sendInfo(jo.toString(), null);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}

		}
		/* System.out.println("*********************"+i); */
	}
}
